package com.csu.cphone.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csu.common.exception.WebException;
import com.csu.common.vo.ResultVo;
import com.csu.cphone.pojo.RecycleOrderPojo;
import com.csu.cphone.service.RecycleOrderService;
import com.csu.cphone.vo.RecycleOrderVo;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @项目名称：cphone
 * @类名称：OrderController
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月14日 下午7:10:12
 * @company:
 */
@Controller
@RequestMapping("/recycle/order")
@Api(value = "recycle/order", description = "回收订单信息")
public class RecycleOrderController
{

    @Autowired
    private RecycleOrderService orderService;

    @ResponseBody
    @ApiOperation(value = "提交订单", notes = "用户填写订单信息完成后，提交订单")
    @RequestMapping(value = "/commitOrder", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<RecycleOrderPojo> commitOrder(HttpServletRequest request, HttpServletResponse response,
            @RequestBody RecycleOrderVo orderVo) throws WebException
    {
        ResultVo<RecycleOrderPojo> resultVo = new ResultVo<RecycleOrderPojo>();

        // 提交订单
        RecycleOrderPojo result = orderService.commitOrder(orderVo);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "更新订单", notes = "服务方接受并更新订单")
    @RequestMapping(value = "/updateOrder", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<RecycleOrderPojo> updateOrder(HttpServletRequest request, HttpServletResponse response,
            @RequestBody RecycleOrderPojo orderPojo)
    {
        ResultVo<RecycleOrderPojo> resultVo = new ResultVo<RecycleOrderPojo>();
        // 更新订单
        RecycleOrderPojo result = orderService.updateOrder(orderPojo);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "转派订单", notes = "转派并更新订单")
    @RequestMapping(value = "/turnOrder", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<RecycleOrderPojo> turnOrder(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "待转派订单sn") @RequestParam(value = "sn", required = true) String sn,
            @ApiParam(value = "待接受服务方的电话") @RequestParam(value = "number", required = true) String number)
            throws WebException
    {
        ResultVo<RecycleOrderPojo> resultVo = new ResultVo<RecycleOrderPojo>();
        // 更新订单
        RecycleOrderPojo result = orderService.turnOrder(sn, number);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "完成订单", notes = "服务方完成订单")
    @RequestMapping(value = "/finishOrder/{serialNumber}", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> finishOrder(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "流水号") @PathVariable(value = "serialNumber") String serialNumber)
    {
        ResultVo<Integer> resultVo = new ResultVo<Integer>();
        // 更新订单
        Integer result = orderService.finishOrder(serialNumber);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "确认订单完成", notes = "用户确认订单完成")
    @RequestMapping(value = "/confirmFinish/{serialNumber}", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> confirmFinish(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "流水号") @PathVariable(value = "serialNumber") String serialNumber)
    {
        ResultVo<Integer> resultVo = new ResultVo<Integer>();
        // 更新订单
        Integer result = orderService.confirmFinish(serialNumber);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "查询订单", notes = "依据流水号查询订单信息")
    @RequestMapping(value = "/orderInfo/{serialNumber}", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<RecycleOrderPojo> queryOrderBySn(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "流水号") @PathVariable(value = "serialNumber") String serialNumber)
    {
        ResultVo<RecycleOrderPojo> resultVo = new ResultVo<RecycleOrderPojo>();
        // 查询订单
        RecycleOrderPojo result = orderService.getOrderBySn(serialNumber);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "查询订单列表", notes = "依据用户id查询订单列表信息")
    @RequestMapping(value = "/orderList/{customerId}", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<RecycleOrderPojo>> queryOrderByCustomerId(HttpServletRequest request,
            HttpServletResponse response, @ApiParam(value = "用户id") @PathVariable(value = "customerId") int customerId)
    {
        ResultVo<List<RecycleOrderPojo>> resultVo = new ResultVo<List<RecycleOrderPojo>>();
        // 查询订单
        List<RecycleOrderPojo> result = orderService.queryOrdersByCustomerId(customerId);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "查询订单列表", notes = "依据服务方电话查询订单列表信息")
    @RequestMapping(value = "/orderListByProducer", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<RecycleOrderPojo>> getOrderByProducer(HttpServletRequest request,
            @ApiParam(value = "服务方电话") @RequestParam(value = "number", required = true) String number,
            @ApiParam(value = "订单状态") @RequestParam(value = "Status", required = false) Integer Status)
    {
        ResultVo<List<RecycleOrderPojo>> resultVo = new ResultVo<List<RecycleOrderPojo>>();
        // 查询订单
        List<RecycleOrderPojo> result = orderService.queryOrderByProducer(number, Status);
        resultVo.setData(result);

        return resultVo;
    }

    @ResponseBody
    @ApiOperation(value = "获取待接收的订单", notes = "服务方查看待接收的订单sn，在线时需要轮询")
    @RequestMapping(value = "/getWaitingOrder/{number}", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<RecycleOrderPojo>> getWaitingOrder(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "服务方电话") @PathVariable(value = "number", required = true) String number)
    {
        ResultVo<List<RecycleOrderPojo>> result = new ResultVo<List<RecycleOrderPojo>>();
        List<RecycleOrderPojo> data = orderService.getWaitingOrder(number);
        result.setData(data);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "作废订单", notes = "用户可以废除自己的未接收或者未处理订单")
    @RequestMapping(value = "/cacnleOrder/{orderSn}/{customerId}", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Integer> cacnleOrder(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "订单id") @PathVariable(value = "orderSn", required = true) String orderSn,
            @ApiParam(value = "用户id") @PathVariable(value = "customerId", required = true) int customerId)
    {
        ResultVo<Integer> result = new ResultVo<Integer>();
        Integer data = orderService.cancelOrder(orderSn, customerId);
        result.setData(data);
        return result;
    }
}
